#!/bin/bash

ulimit -n 10240


mkdir -p /data/hadoop/tmp
node=${hostname}

echo "current node: ${node} ,role ${node_role}"

/usr/sbin/sshd

if [[ ${node_role} == "Master" ]]
then
    [[ ! -e "/data/hadoop/init.done" ]] && /opt/hadoop/bin/hadoop namenode -format && touch /data/hadoop/init.done

    /opt/hadoop/sbin/start-all.sh

    cat ${HADOOP_HOME}/etc/hadoop/slaves|while read line;do
        ssh ${line} touch /data/hadoop/init.done
    done
    sleep 2
    echo "####### bootstrap hbase now"
    ${HBASE_HOME}/bin/start-hbase.sh


elif [[ ${node_role} == "Slave" ]]
then
    if [[  -e "/data/hadoop/init.done" ]]
    then
        echo "Recovery from downtime"
        /opt/hadoop/sbin/start-all.sh 
        ${HBASE_HOME}/bin/start-hbase.sh   
    else 
        echo "Initalzing ... "
        echo "Wait for master to bootstrap"
        sleep 5    
    fi
    # /opt/hadoop/sbin/start-all.sh
fi

echo "######## HBase Started ############"

tail -f 